
/**
 * 修改记录:
 * 		2012.03.07:
 * 			- resumeAnimator 函数中的 anim.start 改为 anim.resume.
 */

package iks.utils
{
	import flash.display.*;
	import flash.geom.*;
	import flash.utils.*;
	import iks.iface.*;
	import iks.render.anim.*;
	
	public final class Helper
	{
		/**
		 * 移除容器中的显示对象, 如果是显示对象是MC, 也stop它
		 * @param	container 移除显示对象的容器
		 */
		public static function removeChildren(container : DisplayObjectContainer) : void
		{
			var i : int = container.numChildren - 1;
			while (i != -1)
			{
				var child : DisplayObject = container.getChildAt(i);
				
				if (child is MovieClip)
					MovieClip(child).stop();
				
				if (child is IKDispose)
					IKDispose(child).dispose();
					
				if (container.contains(child))
					container.removeChildAt(i);
				
				--i;
			}
			
			if (container is MovieClip)
				MovieClip(container).stop();
				
			if (container is IKDispose)
				IKDispose(container).dispose();
		}
		
		/**
		 * 暂停容器内的动画, 包括其子容器
		 * @param	container 暂停动画的容器
		 */
		public static function pauseAnimator(container : DisplayObjectContainer) : void
		{
			for (var i : int = container.numChildren - 1; i >= 0; --i)
			{
				var obj : DisplayObject = container.getChildAt(i);
				var anim : KSpriteAnimator = obj as KSpriteAnimator;
				if (anim != null)
				{
					anim.pause();
				}
				else if (obj is DisplayObjectContainer)
				{
					pauseAnimator(obj as DisplayObjectContainer);
				}
			}
		}
		
		/**
		 * 继续开始容器内的动画, 包括其子容器
		 * @param	container 暂停动画的容器
		 */
		public static function resumeAnimator(container : DisplayObjectContainer) : void
		{
			for (var i : int = container.numChildren - 1; i >= 0; --i)
			{
				var obj : DisplayObject = container.getChildAt(i);
				var anim : KSpriteAnimator = obj as KSpriteAnimator;
				if (anim != null)
				{
					anim.resume();
				}
				else if (obj is DisplayObjectContainer)
				{
					resumeAnimator(obj as DisplayObjectContainer);
				}
			}
		}
		
		/**
		 * 获得半分比
		 * @param	curValue 分子
		 * @param	maxValue 分母
		 */
		public static function getPercent(curValue : Number, maxValue : Number) : Number
		{
			return curValue / maxValue;
		}
		
		/// Eular
		public static function moveEular(elapse : Number, pos : Point, vel : Point, acc : Point) : void
		{
			pos.x += vel.x * elapse;
			pos.y += vel.y * elapse;
			
			vel.x += acc.x * elapse;
			vel.y += acc.y * elapse;
		
		}
		
		/// RK2
		public static function moveRK2(elapse : Number, pos : Point, vel : Point, acc : Point) : void
		{
			pos.x += vel.x * elapse + 0.5 * acc.x * elapse * elapse;
			pos.y += vel.y * elapse + 0.5 * acc.y * elapse * elapse;
			
			vel.x = vel.x + acc.x * elapse;
			vel.y = vel.y + acc.y * elapse;
		
		}
		
		/// RK4
		public static function moveRK4(elapse : Number, pos : Point, vel : Point, acc : Point) : void
		{
			// vel1
			var vel1 : Point = vel;
			var acc1 : Point = acc;
			
			// vel2
			var vel2 : Point = new Point();
			vel2.x = vel.x + acc1.x / 2 * elapse;
			vel2.y = vel.y + acc1.y / 2 * elapse;
			var acc2 : Point = acc;
			
			// vel3
			var vel3 : Point = new Point();
			vel3.x = vel.x + acc2.x / 2 * elapse;
			vel3.y = vel.y + acc2.y / 2 * elapse;
			var acc3 : Point = acc;
			
			// vel4
			var vel4 : Point = new Point();
			vel4.x = vel.x + acc3.x * elapse;
			vel4.y = vel.y + acc3.y * elapse;
			var acc4 : Point = acc;
			
			pos.x += (vel1.x + vel2.x * 2 + vel3.x * 2 + vel4.x) / 6 * elapse;
			pos.y += (vel1.y + vel2.y * 2 + vel3.y * 2 + vel4.y) / 6 * elapse;
			
			vel.x += (acc1.x + acc2.x * 2 + acc3.x * 2 + acc4.x) / 6 * elapse;
			vel.y += (acc1.y + acc2.y * 2 + acc3.y * 2 + acc4.y) / 6 * elapse;
		
		}
	
		/**
		 * 将字节序列转为字符串
		 * @param	buffer 字节序列
		 * @param	dataLen 记录字符串长度的使用的字节数: 0表示没有使用字节长度, 其他分别为1, 2, 3, 4
		 * @return 字符串
		 */
		public static function decodeString(buffer : ByteArray, dataLen : int) : String
		{
			if (dataLen < 0 || dataLen > 4)
				throw new ArgumentError("dataLen只能使用0, 1, 2, 3, 4");
				
			if (buffer == null)
				throw new ArgumentError("参数不能为null");
				
			if (dataLen == 0)
				return buffer.readUTFBytes(buffer.bytesAvailable);
				
			var len : int = 0;
			switch (dataLen)
			{
				case 1:
					len = buffer.readUnsignedByte();
					break;
					
				case 2:
					len = buffer.readUnsignedShort();
					break;
					
				case 3:
					len = (buffer.readUnsignedByte() << 16) | buffer.readUnsignedShort();
					break;
					
				case 4:
					len = buffer.readUnsignedInt();
					break;
					
				default:
					break;
			}
			
			return buffer.readUTFBytes(len);
		}
		
		/**
		 * 字符串转化成字节序列, 可以指定记录字符串长度的字节长度
		 * @param	temp 字符串
		 * @param	buffer 写入的字节序列
		 * @param	dataLen 记录字符串字节的长度
		 */
		public static function encodeString(temp : String, buffer : ByteArray, dataLen : int) : void
		{
			if (dataLen < 0 || dataLen > 4)
				throw new ArgumentError("只能使用0, 1, 2, 3, 4");
				
			if (temp == null || buffer == null)
				throw new ArgumentError("参数不能为null");
				
			if (dataLen == 0)
			{
				buffer.writeUTFBytes(temp);
				return;
			}
			
			var bytes : ByteArray = new ByteArray();
			bytes.writeUTFBytes(temp);
			var len : int = bytes.length;
			
			switch (dataLen)
			{
				case 1:
					if (len > 255)
						len = 255;
					buffer.writeByte(len);
					break;
					
				case 2:
					if (len > 65535)
						len = 65535;
					buffer.writeShort(len);
					break;
					
				case 3:
					if (len > 16777215)
						len = 16777215;
					buffer.writeByte(len >>> 16);
					buffer.writeShort(len);
					break;
					
				case 4:
					if (len > 2147483647)
						len = 2147483647;
					buffer.writeInt(len);
					break;
					
				default:
					break;
			}
			
			buffer.writeBytes(bytes, 0, len);
		}
		
		/**
		 * 将传入的字节进行异或操作
		 * @param	inBytes 传入的字节
		 * @return 进行异或后的字节
		 */
		public static function xorBytes(inBytes : ByteArray) : ByteArray
		{
			var outBytes : ByteArray = new ByteArray();
			while (inBytes.bytesAvailable != 0)
			{
				var byte : int = inBytes.readUnsignedByte() ^ 0xFF;
				outBytes.writeByte(byte);
			}
			
			outBytes.position = 0;
			return outBytes;
		}
	}
}


































///////////////////////////////////////////////////////////////////////////////////////////////////
// end file


